/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LogginPanel.java
 *
 * Created on 21 mars 2012, 08:19:06
 */
package fr.insa.glaizot;


import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author 4glaitan
 */
public class LogginPanel extends javax.swing.JPanel implements WindowListener {

    /** Creates new form LogginPanel */
    public LogginPanel() {
        initComponents();
    }

    public static void main(String[] args) {
        TestSGBD.defautCon = TestSGBD.etabliConnection();
        LogginPanel pan = new LogginPanel();
        JFrame f = TestePanel.pack("Login", pan);
        f.addWindowListener(pan);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTFNom = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTAMessage = new javax.swing.JTextArea();
        jBvalider = new javax.swing.JButton();

        jLabel1.setText("Nom");

        jTFNom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFNomActionPerformed(evt);
            }
        });

        jTAMessage.setColumns(20);
        jTAMessage.setRows(5);
        jScrollPane1.setViewportView(jTAMessage);

        jBvalider.setText("Valider");
        jBvalider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBvaliderActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTFNom, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE))
                    .addComponent(jBvalider))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTFNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jBvalider)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTFNomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFNomActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTFNomActionPerformed

    private void jBvaliderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBvaliderActionPerformed
        try {
            PreparedStatement Stat = (PreparedStatement) TestSGBD.defautCon.prepareStatement("SELECT PRENOM, SALAIRE FROM EMPLOYE WHERE NOM = ?");
            String nom = this.jTFNom.getText();
            Stat.setString(1, nom);
            ResultSet result = Stat.executeQuery();


            if (result.next()) { //y'a au moins une ligne
                String prenom = result.getString("PRENOM");


                double salaire = result.getDouble("SALAIRE");


                this.jTAMessage.append("Bonjour Monsieur/Madame " + prenom + " " + nom + "\nVous gagnez " + salaire + "€\n");


            } else {
                this.jTAMessage.append("Je n'ai pas l'honneur et le privilège de vous connaitre :'( \n");


            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Problème  -_-'  " + ex);


        }
    }//GEN-LAST:event_jBvaliderActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBvalider;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTAMessage;
    private javax.swing.JTextField jTFNom;
    // End of variables declaration//GEN-END:variables

    

    public void windowOpened(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {
        if (TestSGBD.defautCon != null) {
            try {
                System.out.println("connexion fermée !");
                TestSGBD.defautCon.close();
            } catch (SQLException ex) {
                System.out.println("problème" + ex);
            }
        }
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }
}
